﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.Services;
using ICSharpCode.SharpZipLib.Zip;

namespace UpdateServer
{
    /// <summary>
    /// UpdateService 的摘要说明
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    public class UpdateService : System.Web.Services.WebService
    {



        [WebMethod(Description = "测试版本更新服务.")]
        public string UpdateHelloWorld()
        {
            return "Hello World";
        }

        [WebMethod(Description = "获取最新版本.")]
        public string GetLastVersion(string appID)
        {
            ServerDataTableAdapters.T_APPSTableAdapter ad = new UpdateServer.ServerDataTableAdapters.T_APPSTableAdapter();
            ServerData.T_APPSDataTable apps = ad.GetDataByAPPID(appID);
            if (apps.Count > 0)
            {
                return apps[0].LASTVERSION;
            }
            else
            {
                return string.Empty;
            }
           
        }

        [WebMethod(Description = "获取最新文件地址.")]
        public string GetLastFileUrl(string versionID)
        {
            ServerDataTableAdapters.T_APPS_VERSIONTableAdapter ad = new UpdateServer.ServerDataTableAdapters.T_APPS_VERSIONTableAdapter();
            ServerData.T_APPS_VERSIONDataTable versions = ad.GetDataByVersionID(versionID);
            if (versions.Count > 0)
            {
                return versions[0].UPDATEFILEURL;
            }
            else
            {
                return string.Empty;
            }
           
        }

        [WebMethod(Description = "获取最新文件列表.")]
        public ServerData.T_VERSION_FILEDataTable GetLastUpdateFiles(string versionID)
        {
            ServerData.T_VERSION_FILEDataTable tmp = new ServerData.T_VERSION_FILEDataTable();
            ServerDataTableAdapters.T_VERSION_FILETableAdapter ad = new UpdateServer.ServerDataTableAdapters.T_VERSION_FILETableAdapter();
            ad.FillByVersionID(tmp, versionID);
            return tmp;
        }


        [WebMethod(Description = "获取最新文件包名称.")]
        public string GetLastUpdatePackageName(string versionID)
        {
            ServerDataTableAdapters.T_APPS_VERSIONTableAdapter ad = new UpdateServer.ServerDataTableAdapters.T_APPS_VERSIONTableAdapter();
            ServerData.T_APPS_VERSIONDataTable versions = ad.GetDataByVersionID(versionID);
            if (versions.Count > 0)
            {
                return versions[0].VERSIONNAME;
            }
            else
            {
                return string.Empty;
            }
        }


        /// <summary>
        /// 压缩文件
        /// </summary>
        /// <param name="FileToZip"></param>
        /// <param name="ZipedFile"></param>
        /// <returns></returns>
        public static Boolean ZipFile(string FileToZip, string ZipedFile)
        {

            try
            {
                FastZip fastZip = new FastZip();

                bool recurse = true;

                //压缩后的文件名，压缩目录 ，是否递归          

                fastZip.CreateZip(ZipedFile, FileToZip, recurse, "");
                return true;
            }
            catch { return false; }

        }
        /// <summary>
        /// 解压
        /// </summary>
        /// <param name="zipFile"></param>
        /// <param name="targetPath"></param>
        public static Boolean UnZipFile(string zipFile, string targetPath)
        {
            try
            {
                FastZip fastZip = new FastZip();
                fastZip.ExtractZip(zipFile, targetPath, "");
                return true;
            }
            catch { return false; }
        }


        /// <summary>
        /// 弹出JavaScript小窗口
        /// </summary>
        public static void Alert(string message)
        {
            string js = "<script language='JavaScript'>alert('{0}');history.go(-1);</script>";
            HttpContext.Current.Response.Write(string.Format(js, message));
        }

        public static ServerData.T_USERRow GetUserByUID(string uid)
        {
            ServerDataTableAdapters.T_USERTableAdapter ad = new UpdateServer.ServerDataTableAdapters.T_USERTableAdapter();
            ServerData.T_USERDataTable users = ad.GetDataByUID(uid);
            if (users.Count > 0) {
                return users[0];
            }
            return null;
        }
    }
}
